Module# 04: Arrays Lecture#14: Arrays for Arrays
// Example 14.1:
Converting an array to a collection object
// This program
illustrates the use of asList() method.
import java.util.Arrays;
public class ArrayToListDemo {
public static void main(String[] args) {
int intArr[] = { 10, 20, 15, 22, 35}; // An array of int
System.out.println("Integer Array as List: "
+ Arrays.asList(intArr)); // To convert the elements as List
}
}
// Example 14.2:
Stream representation of arrays
/* This method
returns a sequential stream with the specified array as its source. */
import java.util.Arrays;
public class ArraysToStreamDemo {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
// To get the Stream from the array
System.out.println("Integer Array: "
+ Arrays.stream(intArr));
}
}
// Example 14.3:
Converting array items into String objects
/* This method
returns a String representation of the contents of this Arrays. */
import java.util.Arrays;
public class ArraysToStringDemo {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
// To print the elements in one line
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
// Example 14.4:
Converting array items into String objects
// This program
illustrates the use of Arrays.deepToString() method.
import java.util.Arrays;
public class
ArraysToDeepStringDemo {
public static void main(String[] args){
// Get the Array
int intArr[][] = { { 10, 20, 15, 22, 35} };
// To get the deep String of the arrays
System.out.println("Integer Array: "
+ Arrays.deepToString(intArr));
}
}
// Example 14.5:
Copying an array to collection
// This program
illustrates the use of copyOf() method.
import java.util.Arrays;
public class CopyOfArraysDemo {
public static void main(String[] args) {
int intArr[] = { 10, 20, 15, 22, 35}; // An input array
// To print the elements in one line
System.out.println("Integer Array: ” +
Arrays.toString(intArr));
System.out.println("\nNew Arrays by copyOf:\n");
System.out.println("Integer Array: "
+ Arrays.toString( Arrays.copyOf(intArr, 10)));
}
}
// Example 14.6:
Copying a range of an array to collection
// This program
illustrates the use of Arrays.copyOfRange() method.
import java.util.Arrays;
public class RangeCopyArraysDemo {
public static void main(String[] args){
int intArr[] = { 10, 20, 15, 22, 35}; // Get an array of int
// To print the elements in one line
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
System.out.println("\nNew Arrays by copyOfRange:\n");
// To copy the array into an array of new
length
System.out.println("Integer Array: "
+ Arrays.toString(
Arrays.copyOfRange(intArr, 1, 3)));
}
}
// Example 14.7:
Comparing two arrays
// This program
illustrates the use of Arrays.deepEquals() method
import java.util.Arrays;
public class CompareArraysDemo {
public static void main(String[] args) {
int intArr1[][] = { { 10, 20, 15, 22, 35} }; // An input array
int intArr2[][] = { { 10, 15, 22} }; // Another input array
// To compare both the arrays
System.out.println("Integer Arrays on comparison: "
+ Arrays.deepEquals(intArr1, intArr2));
}
}
// Example 14.8:
Hashcode generation of array items
//This program
illustrates the use of Arrays.deepHashCode() method.
import java.util.Arrays;
public class ArraysOfHashcodeDemo
{
public static void main(String[] args){
// Get the Array
int intArr[][] = { { 10, 20, 15, 22, 35} };
// To get the dep hashCode of the arrays
System.out.println("Integer Array: "
+ Arrays.deepHashCode(intArr));
}
}
// Example 14.9:
Entering an item into an array
// This program
illustrates the use of Arrays.fill() method
import java.util.Arrays;
public class
InsertiontoArraysDemo {
public static void main(String[] args){
// Get the Arrays
int intArr[] = { 10, 20, 15, 22, 35};
int intKey = 22;
Arrays.fill(intArr, intKey);
// To fill the arrays
System.out.println("Integer Array on filling: "
+ Arrays.toString(intArr));
}
}
// Example 14.10:
Binary search on an array
// This program illustrates the use of Binary
Search method.
import java.util.Arrays;
public class BinarySerachArraysDemo
{
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
Arrays.sort(intArr);
int intKey = 22;
System.out.println(intKey
+ " found at index
= "
+ Arrays.binarySearch(intArr, intKey));
}
}
// Example 14.11:
Binary search on a sub-list of an array
/* This program
illustrates the use of Binary Search method within a sub list. */
import java.util.Arrays;
public class
ArraysBinarySerachSubListDemo {
public static void main(String[] args){
int intArr[] = { 10, 20, 15, 22, 35}; // An int array as input
Arrays.sort(intArr); // Sort the array
int intKey = 22;
System.out.println ( intKey + " found at index = "
+ Arrays.binarySearch(intArr, 1, 3, intKey));
}
}
// Example 14.12:
Simple sorting
// This program
illustrates the use of Arrays.sort() method.
import java.util.Arrays;
public class SortingArraysDemo {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// To sort the array using normal sort-
Arrays.sort(intArr);
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
// Example 14.13:
Sorting a sublist
/* This program
illustrates the use of second version of Arrays.sort() method. */
import java.util.Arrays;
public class SortingSublistDemo {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
// To sort the array using normal sort
Arrays.sort(intArr, 1, 3);
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
// Example 14.14:
Parallel sorting
// This program
illustrates the use of Arrays.parallelSort() method.
import java.util.Arrays;
public class ParallelSortDemo {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
// To sort the array using parallelSort
Arrays.parallelSort(intArr);
System.out.println("Integer Array: "
+ Arrays.toString(intArr));
}
}
// Example 14.15:
Comparator method for sorting
// This program
illustrates the use of Comparator
interface
import java.util.*;
import java.lang.*;
import java.io.*;
// A class to
represent a student.
class Student {
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name, String address){
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Overriding toString() for Student class
for printing
public String toString(){
return this.rollno + " ” + this.name + " ” + this.address;
}
} // End of class
Student
class Sortbyroll implements Comparator<Student> {
// Used for sorting in ascending order of
roll number
public int compare(Student a, Student b){
return a.rollno - b.rollno;
}
}
// Driver class
class Sorting{
public static void main(String[] args){
Student[] arr = { new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur") };
System.out.println("Unsorted");
for(int i = 0; i<arr.length; i++)
System.out.println(arr[i]);
Arrays.sort(arr, 1, 2, new Sortbyroll());
System.out.println("\nSorted by rollno");
for(int i = 0; i<arr.length; i++)
System.out.println(arr[i]);
}
}
// Example 14.16:
Traversing an array using spliterator()
/* This method
returns a Spliterator covering all of the specified Arrays. */
import java.util.Arrays;
public class TraverseSpliterator {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
// To sort the array using normal sort
System.out.println("Integer Array: "
+ Arrays.spliterator(intArr));
}
}
// Example 14.17:
Traversal of a sublist of arrays
/* This method
returns a Spliterator covering all of the specified Arrays. */
import java.util.Arrays;
public class ArraysTraversalDemo {
public static void main(String[] args){
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35};
// To sort the array using normal sort
System.out.println("Integer Array: "
+ Arrays.spliterator(intArr, 1, 3));
}
}